/**
 * Project: guahao-portal-biz-core
 * 
 * File Created at 2012-11-8
 * 
 * Copyright 2012 Greenline.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Greenline Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Greenline.com.
 */
package org.xukai.common;

/**
 * @Type TagsList
 * @Desc
 * @author jianyun.zheng
 * @date 2012-11-8
 * @Version V1.0
 */
public class TagsList {

    private String[] data;
    private int size = 0;

    public TagsList(int size) {
        data = new String[size];
    }

    public TagsList() {
        this(10);
    }

    public void add(String str) {
        ensureCapacity(size + 1);
        data[size++] = str;
    }

    public String get(int index) {
        if (index < size)
            return data[index];
        else
            return null;
    }

    // 为了提高效率，只将其置为null
    public boolean remove(String str) {
        for (int index = 0; index < size; index++) {
            if (str.equals(data[index])) {
                data[index] = null;
                return true;
            }
        }
        return false;
    }

    public boolean remove(int index) {
        if (index < data.length) {
            data[index] = null;
            return true;
        }
        return false;
    }

    public int size() {
        return this.size;
    }

    // 扩展容量
    public void ensureCapacity(int minSize) {
        int oldCapacity = data.length;
        if (minSize > oldCapacity) {
            int newCapacity = (oldCapacity * 3 / 2 + 1) > minSize ? oldCapacity * 3 / 2 + 1 : minSize;
            String[] newArray = new String[newCapacity];
            for (int i = 0; i < data.length; i++) {
                newArray[i] = data[i];
            }
            data = newArray;
        }
    }
}
